package com.cskaoyan.order.controller;

import com.alibaba.fastjson.JSON;
import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.mall.commons.result.TokenInfo;
import com.cskaoyan.order.dto.OrderDetailResponse;
import com.cskaoyan.order.dto.OrderListResponse;
import com.cskaoyan.order.form.OrderListRequest;
import com.cskaoyan.order.service.OrderCoreService;
import com.cskaoyan.order.service.OrderQueryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author kiggy
 * @since 2023/01/10 10:45
 */
@RestController
public class OrderOtherController {

    @Autowired
    OrderQueryService queryService;

    @Autowired
    OrderCoreService coreService;

    /**
     * @param orderList
     * @return
     */
    @GetMapping("/shopping/order")
    public ResponseData allOrder(OrderListRequest orderList,@RequestHeader String user_info){
        TokenInfo tokenInfo = JSON.parseObject(user_info, TokenInfo.class);
        orderList.setUserId(tokenInfo.getUid());

        OrderListResponse listResponse = queryService.orderList(orderList);

        ResponseUtil<OrderListResponse> responseUtil = new ResponseUtil<>();
        return responseUtil.setData(listResponse);
    }

    /**
     * @param id
     * @return
     */
    @GetMapping("/shopping/order/{id}")
    public ResponseData orderDetails(@PathVariable("id")String id){
        OrderDetailResponse detailResponse = queryService.orderDetail(id);

        ResponseUtil<OrderDetailResponse> responseUtil = new ResponseUtil<>();
        return responseUtil.setData(detailResponse);
    }

    /**
     * TODO:待做
     * 取消订单，根据订单id改变订单状态
     * data返回null即可
     * @param orderMap
     * @return
     */
    @PostMapping("/shopping/cancelOrder")
    public ResponseData cancelOrder(@RequestBody Map orderMap){
        String orderId = (String) orderMap.get("orderId");
        coreService.cancelOrder(orderId);
        coreService.updateOrder(2,orderId);
        ResponseUtil<Object> responseUtil = new ResponseUtil<>();
        return responseUtil.setData(null);
    }

    /**
     * 根据订单id删除订单
     * data返回null即可
     * @return
     */
    @DeleteMapping("/shopping/order/{id}")
    public ResponseData deleteOrder(@PathVariable("id")String id){
        coreService.deleteOrder(id);
        ResponseUtil<Object> responseUtil = new ResponseUtil<>();
        return responseUtil.setData(null);
    }
}
